//
//  DataTopView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/26.
//

#import "DataTopView.h"
#import "RHPicChartView.h"
@interface DataTopView()
@property (nonatomic, strong) QMUILabel *onlineCountLab;

@property (nonatomic, strong) QMUILabel *onlinePercentLab;

@property (nonatomic, strong) QMUILabel *offLineCountLab;

@property (nonatomic, strong) QMUILabel *offLinePercentLab;

@property (nonatomic, strong) RHPicChartView *picView;

@property (nonatomic, assign) NSInteger onLine;

@property (nonatomic, assign) NSInteger offLine;
@end
@implementation DataTopView
- (instancetype)initWithFrame:(CGRect)frame
{
    if([super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}
-(instancetype)init
{
    self = [super init];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setOnlienCount:(NSInteger)onLine offlineCount:(NSInteger)offLine;
{
    _onLine = onLine;
    _offLine = offLine;
    self.onlineCountLab.text = [NSString stringWithFormat:@"%ld",(long)onLine];
    self.offLineCountLab.text = [NSString stringWithFormat:@"%ld",(long)offLine];
    double onlinePercent = 0;
    double offLinePercent = 0;
    if(_onLine + _offLine !=0){
        NSInteger value = onLine;
        //以数量大的为准
        if (offLine>onLine) {
            value = offLine;
        }
        onlinePercent = (value*1.0/(_onLine+_offLine)*100.0);
        NSString *onlineStr = [NSDecimalNumber decimalPlacesWithoutRoundDownWithDigit:2 value:onlinePercent];

        NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithString:@"100"];
        NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDouble:[onlineStr doubleValue]];
        NSDecimalNumber *result = [A decimalNumberBySubtracting:B];
        NSString *offLineStr = [NSString stringWithFormat:@"%@",result];
        self.onlinePercentLab.text = [NSString stringWithFormat:@"%@%%",onlineStr];
        self.offLinePercentLab.text = [NSString stringWithFormat:@"%@%%",offLineStr];
        if (offLine>onLine) {
            self.offLinePercentLab.text = [NSString stringWithFormat:@"%@%%",onlineStr];
            self.onlinePercentLab.text = [NSString stringWithFormat:@"%@%%",offLineStr];
        }
    }else{
        self.onlinePercentLab.text = @"0%";
        self.offLinePercentLab.text = @"0%";
    }
    
    
    
    [self.picView setOnlienCount:onLine offlineCount:offLine];
}
-(void)setupUI{
    
    _onLine = 0;
    _offLine = 0;
    self.backgroundColor = [UIColor whiteColor];
    //饼状图
    RHPicChartView *picView = [[RHPicChartView alloc]init];
    [self addSubview:picView];
    self.picView = picView;
    
    //在线
    UIView *onlinePointView = [[UIView alloc]init];
    [self addSubview:onlinePointView];
    onlinePointView.backgroundColor = CMainColorGreen;
    onlinePointView.layer.cornerRadius = 5;
    onlinePointView.layer.masksToBounds = YES;
    
    QMUILabel *onlineLab = [[QMUILabel alloc]init];
    [self addSubview:onlineLab];
    onlineLab.textColor = [UIColor colorWithHexString:@"3f4443"];
    onlineLab.text = @"在线".rh_localized;
    
    QMUILabel *onlineCountLab = [[QMUILabel alloc]init];
    [self addSubview:onlineCountLab];
    onlineCountLab.textColor = [UIColor colorWithHexString:@"3f4443"];
    onlineCountLab.text = @"0";
    self.onlineCountLab = onlineCountLab;
    
    QMUILabel *onlinePercentLab = [[QMUILabel alloc]init];
    [self addSubview:onlinePercentLab];
    onlinePercentLab.textColor = [UIColor colorWithHexString:@"3f4443"];
    onlinePercentLab.text = @"0%";
    self.onlinePercentLab = onlinePercentLab;
    
    //离线
    UIView *offLinePointView = [[UIView alloc]init];
    [self addSubview:offLinePointView];
    offLinePointView.backgroundColor = [UIColor colorWithHexString:@"EBEEF1"];
    offLinePointView.layer.cornerRadius = 5;
    offLinePointView.layer.masksToBounds = YES;
    
    QMUILabel *offLineLab = [[QMUILabel alloc]init];
    [self addSubview:offLineLab];
    offLineLab.textColor = [UIColor colorWithHexString:@"3f4443"];
    offLineLab.text = @"离线".rh_localized;
    
    QMUILabel *offLineCountLab = [[QMUILabel alloc]init];
    [self addSubview:offLineCountLab];
    offLineCountLab.textColor = [UIColor colorWithHexString:@"3f4443"];
    offLineCountLab.text = @"0";
    self.offLineCountLab = offLineCountLab;
    
    QMUILabel *offLinePercentLab = [[QMUILabel alloc]init];
    [self addSubview:offLinePercentLab];
    offLinePercentLab.textColor = [UIColor colorWithHexString:@"3f4443"];
    offLinePercentLab.text = @"0%";
    self.offLinePercentLab = offLinePercentLab;
    
    UIImageView *bgImg = [[UIImageView alloc]init];
    [self addSubview:bgImg];
    UIImage *img = [UIImage imageNamed:@"data_top_bg"];
    bgImg.image = img;
    //布局
    [picView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(picView.mas_width);
        make.left.equalTo(self).offset(kScaleW(30));
        make.right.equalTo(self.mas_centerX).offset(-kScaleW(30));
        make.centerY.equalTo(self).offset(-20);
    }];
    //灰色小圆点
    [offLinePointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(10);
        make.centerY.equalTo(self).offset(kScaleW(10));
        make.width.offset(kScaleW(15));
        make.height.equalTo(offLinePointView.mas_width);
    }];
    //off-line
    [offLineLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(offLinePointView.mas_right).offset(kScaleW(10));
        make.centerY.equalTo(offLinePointView);
    }];
    //离线数量
    [offLineCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(offLineLab.mas_right).offset(20);
        make.centerY.equalTo(offLineLab);
    }];
    //离线数量百分比
    [offLinePercentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(offLineLab.mas_bottom).offset(10);
        make.left.equalTo(offLineLab);
    }];
    
    
    [onlineCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(onlineLab.mas_right).offset(20);
        make.top.equalTo(self).offset(20);
        make.centerY.equalTo(onlineLab);
    }];
    [onlineLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(onlinePointView.mas_right).offset(kScaleW(10));
        make.centerY.equalTo(onlinePointView);
    }];
    [onlinePointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(offLinePointView);
        make.top.equalTo(picView).offset(kScaleW(10));
        make.width.offset(kScaleW(15));
        make.height.equalTo(onlinePointView.mas_width);
    }];
    [onlinePercentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(onlineLab.mas_bottom).offset(10);
        make.left.equalTo(onlineLab);
    }];
    
    CGFloat width= img.size.width;
    CGFloat height = img.size.height;
    [bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
        make.height.offset(kUI_WINDOW_W*height/width);
    }];
}



@end
